<?php
session_start();
ob_start();
$host="localhost";
$username="root";
$password="";
$database="cs4400_group17";

mysql_connect($host,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$username = $_SESSION['userName'];
$useremail = $_SESSION['userEmail'];

$description=$_GET['description'];
$corkBoardTitle=$_GET['corkBoardTitle'];
$tags = $_GET['tags'];

if($description != "" && $corkBoardTitle != "" && $tags !=""){
	$sqlOne = "SELECT DISTINCT Description , Title , Name , URL, DateTime, Email FROM tags NATURAL JOIN pushpin NATURAL JOIN User WHERE Description LIKE '%$description%' AND  Title LIKE '%$corkBoardTitle%' AND  Tag = '$tags'";
	$resultOne = mysql_query($sqlOne);
	
}
else if ($corkBoardTitle != "" && $tags !="" && $description == ""){
	$sqlOne = "SELECT DISTINCT Description , Title , Name , URL, DateTime, Email FROM tags NATURAL JOIN pushpin NATURAL JOIN User WHERE Title LIKE '%$corkBoardTitle%' AND  Tag = '$tags'";
	$resultOne = mysql_query($sqlOne);
}
else if ($corkBoardTitle != "" && $tags =="" && $description != ""){
	$sqlOne = "SELECT DISTINCT Description , Title , Name , URL, DateTime, Email FROM tags NATURAL JOIN pushpin NATURAL JOIN User WHERE Title LIKE '%$corkBoardTitle%' AND Description LIKE '%$description%' ";
	$resultOne = mysql_query($sqlOne);
}
else if ($corkBoardTitle == "" && $tags !="" && $description != ""){
	$sqlOne = "SELECT DISTINCT Description , Title , Name , URL, DateTime, Email FROM tags NATURAL JOIN pushpin NATURAL JOIN User WHERE Description LIKE '%$description%' AND  Tag = '$tags'";
	$resultOne = mysql_query($sqlOne);
}
else if ($corkBoardTitle == "" && $tags =="" && $description != ""){
	$sqlOne = "SELECT  DISTINCT Description , Title , Name , URL, DateTime, Email FROM ((tags NATURAL JOIN pushpin) NATURAL JOIN User) WHERE Description LIKE '%$description%'";
	$resultOne = mysql_query($sqlOne);
}
else if ($corkBoardTitle != "" && $tags =="" && $description == ""){
	$sqlOne = "SELECT DISTINCT Description , Title , Name , URL, DateTime, Email FROM tags NATURAL JOIN pushpin NATURAL JOIN User WHERE Title LIKE '%$corkBoardTitle%'";
	$resultOne = mysql_query($sqlOne);
}
else if ($corkBoardTitle == "" && $tags !="" && $description == ""){
	$sqlOne = "SELECT DISTINCT Description , Title , Name , URL, DateTime, Email FROM tags NATURAL JOIN pushpin NATURAL JOIN User WHERE Tag = '$tags'";
	$resultOne = mysql_query($sqlOne);
}
else if ($corkBoardTitle == "" && $tags =="" && $description == ""){
	$sqlOne = "SELECT DISTINCT Description , Title , Name , URL, DateTime, Email FROM tags NATURAL JOIN pushpin NATURAL JOIN User";
	$resultOne = mysql_query($sqlOne);
}
?>
<!DOCTYPE html>
<head>
</head>
<body>
	<!--<table width="700" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
		<tr>
			<td>Description</td>
			<td>|</td>
			<td>CorkBoard Title</td>
			<td>|</td>
			<td> Owner Name</td>
		</tr>
	</table>-->
	<form name = "input" action = "home.php" method = "post">
		<input type="submit" value="Back to Home" name="backtohome"/>
	</form>
<?php
while ($rows = mysql_fetch_array($resultOne)){
?>

	<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
			<tr>
				<td>
					<form action='ViewPushpin.php' method='post'>
						<input type="hidden" name="pushpinEmail" value="<?php echo $rows['Email'];?>">
						<input type="hidden" name="pushpinTitle" value="<?php echo $rows['Title'];?>">
						<table width="400" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
							<tr>
								<td width="117">Title</td>
								<td width="14">:</td>
								<td width="357"><?php echo $rows['Title']; ?></td>
							</tr>
							<tr>
								<td>Picture</td>
								<td>:</td>
								<td><img src="<?php echo $rows['URL']; ?>"/>
									<input type="hidden" name="pushpinURL" value="<?php echo $rows['URL'];?>"></td>
							</tr>
							<tr>
								<td width="117">Author</td>
								<td width="14">:</td>
								<td width="357"><?php echo $rows['Name'].' ('.$rows['Email'].')'; ?></td>
							</tr>
							<tr>
								<td>Date</td>
								<td>:</td>
								<td><?php echo $rows['DateTime']; ?>
									<input type="hidden" name="pushpinDateTime" value="<?php echo $rows['DateTime'];?>"></td>
							</tr>
							<tr>
								<td width="117">Description</td>
								<td width="14">:</td>
								<td width="357"><?php echo $rows['Description']; ?>
									<input type="hidden" name="pushpinDescription" value="<?php echo $rows['Description'];?>"></td>
							</tr>
							<tr>
								<td></td>
								<td></td>
								<td><input type="submit" value="Go to Pushpin"/></td>
							</tr>
						</table>
					</form>
				</td>
			</tr>
		</table>
		<BR>
<?php
			}
			mysql_close();
		
			$count=mysql_num_rows($resultOne);
			
			if($count < 1){
				echo "<p>No Pushpins to match your search! Sowwy! :(</p>";
			}
?>
<form name = "input" action = "home.php" method = "post">
		<input type="submit" value="Back to Home" name="backtohome"/>
</form>
</body>
</html